package com.xunshibao.tutor.user.dao;

import org.springframework.stereotype.Repository;

import com.xunshibao.core.dao.BaseDAO;
import com.xunshibao.tutor.po.Tutor;

@Repository
public class TutorDaoImpl {
	private static final String INSERT = "insert into w_tutor(id, school, years, cdt,iscert, gender,tp,edu,byear,uname,area,barea,avator) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
	private static final String SELECT_BY_ID = "select id, school, years, cdt,iscert, gender,tp,edu,byear,uname,area,barea,avator from w_tutor where id=?";
	public int save(Tutor tutor) {
		return BaseDAO.execute(INSERT, new Object[]{tutor.getId(), tutor.getSchool(), tutor.getYears(), tutor.getCdt(), tutor.getIscert(), tutor.getGender(), tutor.getTp(), tutor.getEdu(), tutor.getByear(), tutor.getUname(), tutor.getArea(), tutor.getBarea(), tutor.getAvator()});
	}
	
	public Tutor getById(Long id) {
		return BaseDAO.queryForObject(Tutor.class, SELECT_BY_ID, new Object[]{id});
	}
	
	/**
	 *  update(new String[]{"username", "password"}, new Object[]{"haiker", "nice"}, null);
	 *	update(new String[]{"username", "password"}, new Object[]{"haiker", "nice", 1}, "id=?");
	 * @param properties
	 * @param values
	 * @param condition
	 * @return
	 */
	public int update(String[] properties, Object[] values, String condition) {
		StringBuilder buffer = new StringBuilder("update ");
		buffer.append(Tutor.TB);
		buffer.append(" set ");
		if(properties != null) {
			int i = 0;
			for(String column : properties) {
				if(i > 0) {
					buffer.append(",");
				}
				buffer.append(column);
				buffer.append("=?");
				i++;
			}
		}
		if(condition != null) {
			buffer.append(" where ");
			buffer.append(condition);
		}
		return BaseDAO.execute(buffer.toString(), values);
	}
}
